package demoMod.stsconsole.utils;

import demoMod.stsconsole.ConsoleEntry;
import demoMod.stsconsole.cards.AbstractCard;
import demoMod.stsconsole.helpers.LocalizedStrings;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class CardSelect {
    public static List<AbstractCard> selectCards(List<AbstractCard> cards, int amount, Predicate<AbstractCard> condition, boolean anyNumber) {
        List<AbstractCard> tmpList;
        List<AbstractCard> ret;
        if (condition != null) {
            tmpList = cards.stream().filter(condition).collect(Collectors.toList());
        } else {
            tmpList = new ArrayList<>(cards);
        }
        if (tmpList.isEmpty() || amount <= 0) return new ArrayList<>();
        if (amount >= tmpList.size() && !anyNumber) {
            return new ArrayList<>(tmpList);
        }
        List<String> selectStrings = LocalizedStrings.getUIStrings("CardSelect");
        boolean completed = false;
        ret = new ArrayList<>();
        while (!completed) {
            int index = 0;
            for (AbstractCard card : tmpList) {
                System.out.println(index + "\t" + card.name);
                index++;
            }
            if (anyNumber) {
                System.out.printf(selectStrings.get(1), amount);
            } else {
                System.out.printf(selectStrings.get(0), amount);
            }
            Scanner sc = ConsoleEntry.getScanner();
            try {
                index = Integer.parseInt(sc.next());
                if (index < 0 || index >= tmpList.size()) {
                    if (index == -1 && anyNumber) {
                        return ret;
                    } else {
                        System.out.println(selectStrings.get(2));
                        continue;
                    }
                }
                ret.add(tmpList.get(index));
            } catch (NumberFormatException e) {
                System.out.println(selectStrings.get(2));
                ret.clear();
                continue;
            }
            int i;
            for (i = 0; i < amount - 1; i++) {
                if (sc.hasNext()) {
                    try {
                        index = Integer.parseInt(sc.next());
                        if (index < 0 || index >= tmpList.size()) {
                            System.out.println(selectStrings.get(2));
                            ret.clear();
                            break;
                        }
                        ret.add(tmpList.get(index));
                    } catch (NumberFormatException e) {
                        System.out.println(selectStrings.get(2));
                        ret.clear();
                        break;
                    }
                } else {
                    if (anyNumber) {
                        return ret;
                    } else {
                        System.out.printf(selectStrings.get(3), amount);
                        ret.clear();
                        break;
                    }
                }
            }
            if (i == amount - 1) {
                completed = true;
            }
        }
        return ret;
    }
}
